//
//  UsefulAlertView.swift
//  newproject
//
//  Created by Pro on 2018/1/17.
//  Copyright © 2018年 Pro. All rights reserved.
//

import UIKit



class UsefulAlertView: UIView {
    
    var ConnectView:UIView!
    
    var cancelButtonCallBack:(() -> ())?
    var confirmButtonCallBack:(() -> ())?
    var tapGestureCallBack:(() -> ())?

    
    var statusForDisPlayOut:Int = 0 {
        didSet{
            if self.statusForDisPlayOut == 0{
                DefaultDisplayView.frame = CGRect(x: 0, y: 0, width: 260, height: 190)
                DisplayContentBgView.addSubview(DefaultDisplayView)
            }else if self.statusForDisPlayOut == 1{
                CheckDisplayView.frame = CGRect(x: 0, y: 0, width: 260, height: 190)
                DisplayContentBgView.addSubview(CheckDisplayView)
                
                CancelBtn.setTitle("取消", for: .normal)
                ConfirmBtn.setTitle("去验车", for: .normal)
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        ConnectView = loadViewFromNib()
        addSubview(ConnectView)
        addConstraints()
        initialSetup()
    }
    

    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        ConnectView = loadViewFromNib()
        addSubview(ConnectView)
        addConstraints()
        initialSetup()
    }
    
    func loadViewFromNib() -> UIView {
        let className = type(of: self)
        let bundle = Bundle(for: className)
        let name = NSStringFromClass(className).components(separatedBy: ".").last
        let nib = UINib(nibName: name!, bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
        return view
    }

    func addConstraints() {
        ConnectView.translatesAutoresizingMaskIntoConstraints = false
        var constraint = NSLayoutConstraint(item: ConnectView, attribute: .leading,
                                            relatedBy: .equal, toItem: self, attribute: .leading,
                                            multiplier: 1, constant: 0)
        addConstraint(constraint)
        constraint = NSLayoutConstraint(item: ConnectView, attribute: .trailing,
                                        relatedBy: .equal, toItem: self, attribute: .trailing,
                                        multiplier: 1, constant: 0)
        addConstraint(constraint)
        constraint = NSLayoutConstraint(item: ConnectView, attribute: .top, relatedBy: .equal,
                                        toItem: self, attribute: .top, multiplier: 1, constant: 0)
        addConstraint(constraint)
        constraint = NSLayoutConstraint(item: ConnectView, attribute: .bottom,
                                        relatedBy: .equal, toItem: self, attribute: .bottom,
                                        multiplier: 1, constant: 0)
        addConstraint(constraint)
    }
    
    //初始化默认属性配置
    func initialSetup(){
        self.backgroundColor = UIColor(red: 0/225.0, green: 0/225.0, blue: 0/225.0, alpha: 0.3)
        AlertContentView.layer.cornerRadius = 5
        AlertContentView.layer.masksToBounds  = true
        CancelBtn.backgroundColor = UIColor.groupTableViewBackground
        CancelBtn.setTitleColor(UIColor.black, for: .normal)
        ConfirmBtn.backgroundColor = UIColor.black
        ConfirmBtn.setTitleColor(.white, for: .normal)
    
    }
    
    
    
    @IBOutlet weak var CancelBtn: UIButton!
    @IBAction func CancelBtnAction(_ sender: UIButton) {
        if cancelButtonCallBack != nil {
            cancelButtonCallBack!()
        }
        
    }
    
    @IBOutlet var ConfirmBtn: UIButton!
    @IBAction func ConfirmBtnAction(_ sender: UIButton) {
        if confirmButtonCallBack != nil {
            confirmButtonCallBack!()
        }
    }
    
    
    @IBAction func tapForBgGrayView(_ sender: UITapGestureRecognizer) {
        if tapGestureCallBack != nil{
            tapGestureCallBack!()
        }
    }
    
    
    
    @IBOutlet weak var DisplayContentBgView: UIView!
    @IBOutlet var CheckDisplayView: UIView!
    @IBOutlet var DefaultDisplayView: UIView!
    @IBOutlet weak var AlertContentView: UIView!
    
    
    
    
}
